<template>
  <span class="bi-click-input-container">
    <span tabindex="-1" class="bi-click-input-main" v-if="showInput">
      <Md-input
        class="bi-click-input"
        ref="input"
        type="textarea"
        autosize
        v-model="innerValue"
      >
      </Md-input>
      <md-button
        size="small"
        class="bi-click-input-button"
        @click="handleCancel"
        >取消</md-button
      >
      <md-button
        size="small"
        type="primary"
        class="bi-click-input-button"
        @click="handleConfirm"
        >确认</md-button
      >
    </span>
    <span class="bi-click-input-text-render" v-else @click="handleTextClick">{{
      value
    }}</span>
  </span>
</template>

<script>
import { MdInput, MdButton } from '@mdfe/medi-ui';

export default {
  props: ['value', 'isActive'],

  data() {
    return {
      defaultValue: this.value,
      showInput: false,
      innerValue: this.value,
    };
  },
  methods: {
    handleCancel() {
      this.$emit('update:change', this.defaultValue);
      this.showInput = false;
    },
    handleConfirm() {
      this.$emit('update:change', this.innerValue);
      this.showInput = false;
    },
    handleTextClick() {
      this.defaultValue = this.value;
      this.innerValue = this.value;
      // 暂时禁掉，不需要
      // this.showInput = true
      // this.$nextTick(() => {
      //   this.$refs.input?.focus?.();
      // });
    },
  },

  watch: {
    isActive(val, oldVal) {
      if (val === false && this.showInput) {
        this.handleConfirm();
      }
    },
  },

  components: {
    MdInput,
    MdButton,
  },
};
</script>

<style lang="scss" scoped>
.bi-click-input {
  font-size: inherit;

  ::v-deep {
    > textarea {
      padding: 0;
    }
  }

  &-container {
    text-decoration-line: inherit;
  }

  &-main {
    display: flex;
    position: relative;
    z-index: 1;
    margin-bottom: var(--md-spacing-2);
  }

  &-button {
    margin-left: var(--md-spacing-2);
    padding: 0;
  }

  &-text-render {
    display: inline-block;
    min-width: 28px;
    white-space: pre-wrap;
    word-break: break-word;
    text-decoration-line: inherit;
  }
}
</style>
